//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     会员提现业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Entity.Json.WebAPI;
using CL.Enum.Common;
using CL.Game.DAL;
using CL.Json.Entity;
using CL.Redis.BLL;
using CL.Tools.Common;
using CL.View.Entity.Game;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CL.Game.BLL
{

    /// <summary>
    /// UsersWithdrawBLL 业务
    /// </summary>
    public class UsersWithdrawBLL
    {
        Log log = new Log("UsersWithdrawBLL");

        UsersWithdrawDAL dal = new UsersWithdrawDAL(DbConnectionEnum.CaileGame);


        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="PayOutStatus"></param>
        /// <returns></returns>
        public bool AuditPayOutStatus(List<long> Ids, int PayOutStatus, int OperaterID)
        {
            return dal.AuditPayOutStatus(Ids, PayOutStatus, OperaterID);
        }
        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="PayOutStatus"></param>
        /// <returns></returns>
        public bool AuditPayOutStatus(long Id, int PayOutStatus, int OperaterID)
        {
            return dal.AuditPayOutStatus(Id, PayOutStatus, OperaterID);
        }
        /// <summary>
        /// 提现成功
        /// </summary>
        /// <param name="Model.Entities.UserPayOut"></param>
        /// <param name="OperatorID"></param>
        /// <returns></returns>
        public bool AuditPayOutSuccess(long PayOutID, int OperaterID)
        {
            bool Rec = dal.AuditPayOutSuccess(PayOutID, OperaterID);
            if (Rec)
                Task.Factory.StartNew(() =>
                {
                    //变更静态数据
                    var Entity = dal.Get(PayOutID);
                    new UsersBLL().Staticdata_Withdraw(Entity.Amount);
                });
            return Rec;
        }
        /// <summary>
        /// 提现失败
        /// </summary>
        /// <param name="Model.Entities.UserPayOut"></param>
        /// <param name="OperatorID"></param>
        /// <returns></returns>
        public bool AuditPayOutFailure(long PayOutID, int OperaterID, string Remark)
        {
            return dal.AuditPayOutFailure(PayOutID, OperaterID, Remark);
        }
        /// <summary>
        /// 报表：提现查询
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="UserName"></param>
        /// <param name="UserID"></param>
        /// <param name="PayOutStatus"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="RecordWithdrawAmount"></param>
        /// <param name="RecordCount"></param>
        /// <returns></returns>
        public List<udv_WithdrawReport> QuertWithdrawReport(DateTime StartTime, DateTime EndTime, string UserName, long UserID, int PayOutStatus, int PageIndex, int PageSize, ref long RecordWithdrawAmount, ref int RecordCount)
        {
            return dal.QuertWithdrawReport(StartTime, EndTime, UserName, UserID, PayOutStatus, PageIndex, PageSize, ref RecordWithdrawAmount, ref RecordCount);
        }



        #region 自定义方法
        /// <summary>
        /// 提现申请
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="BankCode"></param>
        /// <param name="Amount"></param>
        /// <param name="PayPwd"></param>
        /// <returns></returns>
        public JsonResult WithdrawDeposit(long UserCode, long BankCode, long Amount, string PayPwd)
        {
            JsonResult result = null;
            try
            {
                int RecCode = (int)ResultCode.Success;
                long AllowMoney = 0;
                int Count = 0;
                var Rec = this.QueryWithdrawCount(UserCode, ref AllowMoney, ref Count);
                if (Rec == ResultCode.Success)
                {
                    int status = dal.ApplyForWithdraw(UserCode, BankCode, Amount, EncryptAlgorithm.MD5(PayPwd));
                    if (status == 0)
                    {
                        new UsersBLL().ModifyUserBalanceRedis(UserCode, Amount, false);
                        //累计个人提现次数
                        UpdateWithdrawCount(UserCode);
                        RecCode = (int)ResultCode.Success;
                    }
                    else if (status == -1)
                        RecCode = (int)ResultCode.SystemBusy;
                    else if (status == -2)
                        RecCode = (int)ResultCode.InsufficientBalance;
                    else if (status == -3)
                        RecCode = (int)ResultCode.WithdrawAmount;
                    else if (status == -4)
                        RecCode = (int)ResultCode.PayPwdFailure;
                    else
                        RecCode = (int)ResultCode.SystemBusy;
                }
                else
                    RecCode = (int)Rec;
                result = new JsonResult()
                {
                    Code = RecCode,
                    Msg = Common.GetDescription((ResultCode)RecCode)
                };
            }
            catch (Exception ex)
            {
                log.Write("提现申请错误[WithdrawDeposit]：" + ex.StackTrace, true);
                result = new JsonResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy)
                };
            }
            return result;
        }
        /// <summary>
        /// 当日可提现次数查询
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public WithdrawCountResult QueryWithdrawCount(long UserCode)
        {
            WithdrawCountResult result = null;
            try
            {
                int RecCode = (int)ResultCode.Success;
                int Count = 0;
                long AllowMoney = 0;
                long WithdrawMoney = 0;
                var Rec = this.QueryWithdrawCount(UserCode, ref AllowMoney, ref Count);
                if (Rec == ResultCode.Success)
                {
                    var UsersExtend = new UsersExtendBLL().QueryEntityByUserCode(UserCode);
                    if (UsersExtend != null && UsersExtend.WithdrawMoney > 0)
                        WithdrawMoney = UsersExtend.WithdrawMoney;
                }
                else
                    RecCode = (int)Rec;
                result = new WithdrawCountResult()
                {
                    Code = RecCode,
                    Msg = Common.GetDescription((ResultCode)RecCode),
                    AllowMoney = AllowMoney,
                    Count = Count,
                    WithdrawMoney = WithdrawMoney
                };
            }
            catch (Exception ex)
            {
                log.Write("当日可提现次数查询错误[WithdrawDeposit]：" + ex.StackTrace, true);
                result = new WithdrawCountResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy)
                };
            }
            return result;
        }
        protected ResultCode QueryWithdrawCount(long UserCode, ref long Withdraw_AllowMoney, ref int Withdraw_Count)
        {
            Withdraw_AllowMoney = Convert.ToInt64(System.Configuration.ConfigurationManager.AppSettings["WITHDRAWMONEY"] ?? "1000");
            Withdraw_Count = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["WITHDRAWCOUNT"] ?? "3");

            var WithdrawCountEntity = new SystemRedis().WithdrawCount(UserCode);
            if (WithdrawCountEntity == null)
            {
                return ResultCode.Success;
            }
            else
            {
                int DayTime = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
                int DayTime_Ext = Convert.ToInt32(WithdrawCountEntity.Time.ToString("yyyyMMdd"));
                if (DayTime_Ext < DayTime)
                {
                    return ResultCode.Success;
                }
                else
                {
                    if (WithdrawCountEntity.Count < Withdraw_Count)
                    {
                        Withdraw_Count = Withdraw_Count - WithdrawCountEntity.Count;
                        return ResultCode.Success;
                    }
                    else
                    {
                        Withdraw_Count = 0;
                        return ResultCode.WithdrawCount;
                    }
                }
            }
        }
        public void UpdateWithdrawCount(long UserCode)
        {
            long AllowMoney = Convert.ToInt64(System.Configuration.ConfigurationManager.AppSettings["WITHDRAWMONEY"] ?? "1000");
            var WithdrawCountEntity = new SystemRedis().WithdrawCount(UserCode);
            if (WithdrawCountEntity == null)
            {
                WithdrawCountEntity = new udv_WithdrawCount();
                WithdrawCountEntity.AllowMoney = AllowMoney;
                WithdrawCountEntity.Count = 1;
                WithdrawCountEntity.Time = DateTime.Now;
                WithdrawCountEntity.UserID = UserCode;
                new SystemRedis().WithdrawCount(WithdrawCountEntity);
            }
            else
            {
                int DayTime = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
                int DayTime_Ext = Convert.ToInt32(WithdrawCountEntity.Time.ToString("yyyyMMdd"));
                if (DayTime_Ext < DayTime)
                {
                    WithdrawCountEntity.AllowMoney = AllowMoney;
                    WithdrawCountEntity.Count = 1;
                    WithdrawCountEntity.Time = DateTime.Now;
                    WithdrawCountEntity.UserID = UserCode;
                    new SystemRedis().WithdrawCount(WithdrawCountEntity);
                }
                else
                {
                    WithdrawCountEntity.AllowMoney = AllowMoney;
                    WithdrawCountEntity.Count = WithdrawCountEntity.Count + 1;
                    WithdrawCountEntity.Time = DateTime.Now;
                    WithdrawCountEntity.UserID = UserCode;
                    new SystemRedis().WithdrawCount(WithdrawCountEntity);
                }
            }

        }
        #endregion
    }
}
